﻿using System.Collections.Specialized;
using System.Configuration;

namespace SNSGame.SNSGame
{
    /// <summary>
    /// App配置信息基类
    /// </summary>
    public abstract class AppConfigBase
    {
        protected AppConfigBase(string sectionName)
        {
            Config = ConfigurationManager.GetSection(sectionName) as NameValueCollection;
            if (Config == null)
                throw new ConfigurationErrorsException("获取API配置出错，请检查配置文件。");
        }

        protected NameValueCollection Config { get; private set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public virtual string Version
        {
            get { return Config["Version"]; }
        }

        /// <summary>
        /// API基礎URL路徑
        /// </summary>
        public string BaseAPIUrl
        {
            get { return Config["BaseAPIUrl"]; }
        }

        /// <summary>
        /// 应用名称
        /// </summary>
        public string AppName
        {
            get { return Config["AppName"]; }
        }

        /// <summary>
        /// 应用存在于社区的访问链接
        /// </summary>
        public string AppSnsUrl
        {
            get { return Config["AppSnsUrl"]; }
        }

        /// <summary>
        /// 本应用交流社区
        /// </summary>
        public string AppForum
        {
            get { return Config["AppForum"]; }
        }

        /// <summary>
        /// AppId
        /// </summary>
        public string AppId
        {
            get { return Config["AppId"]; }
        }

        /// <summary>
        /// AppKey
        /// </summary>
        public string AppKey
        {
            get { return Config["AppKey"]; }
        }

        /// <summary>
        /// AppSecret
        /// </summary>
        public string AppSecret
        {
            get { return Config["AppSecret"]; }
        }

        /// <summary>
        /// 应用支付Id
        /// </summary>
        public string AppPayId
        {
            get { return Config["AppPayId"]; }
        }

        /// <summary>
        /// 返回类型，xml,json
        /// </summary>
        public string ResultType
        {
            get { return Config["ResultType"]; }
        }

        /// <summary>
        /// 超时，ms,默认30秒，不得低于1秒钟
        /// </summary>
        public int Timeout
        {
            get
            {
                int timeout;
                if( int.TryParse(Config["Timeout"], out timeout))
                {
                    return timeout < 10 ? 30000 : timeout;
                }
                return 30000;
            }
        }

        /// <summary>
        /// 异步委托超时时间,默认30秒，不得低于1秒钟
        /// </summary>
        public int AsyncTimeout
        {
            get
            {
                int timeout;
                if (int.TryParse(Config["AsyncTimeout"], out timeout))
                {
                    return timeout < 10 ? 30000 : timeout;
                }
                return 30000;
            }
        }



    }
}